

package com.idcos.enterprise.portal.web.auto;

// auto generated imports

import com.alibaba.fastjson.JSONObject;
import com.idcos.cloud.biz.common.permisson.OperateAction;
import com.idcos.cloud.web.common.BaseResultVO;
import com.idcos.cloud.web.common.JsonResultUtil;
import com.idcos.enterprise.portal.form.*;
import com.idcos.enterprise.portal.manager.auto.PortalUserOperateManager;
import com.idcos.enterprise.portal.manager.auto.PortalUserQueryManager;
import com.idcos.enterprise.portal.web.auto.excelfilehandler.UserExcelFileHandler;
import com.idcos.enterprise.portal.web.auto.excelfilehandler.UserExcelTempleteHandler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 用户管理
 * web层controller相关的接口自动生成，此文件属于自动生成的，请勿直接修改,具体可以参考codegen工程
 * Generated by <tt>controller-codegen</tt> on 2015-11-07 16:44:40.
 *
 * @author jiaohuizhe
 * @version PortalUserController.java, v 1.1 2015-11-07 16:44:40 jiaohuizhe Exp $
 */

@Controller
@OperateAction("F_PORTAL_USER")
@RequestMapping(value = "/portal/user")
@Api(tags = "03.用户管理的接口", description = "PortalUserController")
public class PortalUserController {

    //========== manager ==========

    @Autowired
    private PortalUserQueryManager portalUserQueryManager;

    @Autowired
    private PortalUserOperateManager portalUserOperateManager;

    @Autowired
    private UserExcelFileHandler userExcelFileHandler;

    @Autowired
    private UserExcelTempleteHandler userExcelTempleteHandler;

    /**
     * 获取某用户信息
     *
     * @param id
     * @return BaseResultVO
     */
    @RequestMapping(method = RequestMethod.GET, value = "/{id}")
    @ResponseBody
    @OperateAction("O_PORTAL_USER_GET")
    public BaseResultVO queryById(@PathVariable String id) {
        return JsonResultUtil.getResult(portalUserQueryManager.queryById(id));

    }

    /**
     * 获取某用户权限信息
     *
     * @param id
     * @return BaseResultVO
     */
    @RequestMapping(method = RequestMethod.GET, value = "/permissions/{id}")
    @ResponseBody
    @OperateAction("O_PORTAL_USER_GET")
    public JSONObject queryPermissionsById(@PathVariable String id,
                                           @RequestParam(name = "appId", required = false) String appId) {
        JSONObject json = new JSONObject();
        json.put("status", "success");
        json.put("message", "null");
        json.put("content", portalUserQueryManager.queryPermissionsById(id, appId).getResultObject());
        return json;

    }

    /**
     * 获取某用户角色信息
     *
     * @param id
     * @return BaseResultVO
     */
    @RequestMapping(method = RequestMethod.GET, value = "/roles/{id}")
    @ResponseBody
    @OperateAction("O_PORTAL_USER_GET")
    public BaseResultVO queryRolesById(@PathVariable String id) {
        return JsonResultUtil.getResult(portalUserQueryManager.queryRolesById(id));

    }

    /**
     * 获取某用户用户组信息
     *
     * @param id
     * @return BaseResultVO
     */
    @RequestMapping(method = RequestMethod.GET, value = "/groups/{id}")
    @ResponseBody
    @OperateAction("O_PORTAL_USER_GET")
    public JSONObject queryGroupsById(@PathVariable String id) {
        JSONObject json = new JSONObject();
        json.put("status", "success");
        json.put("message", "null");
        json.put("content", portalUserQueryManager.queryGroupsById(id));
        return json;
    }

    /**
     * 分配用户组
     *
     * @param form
     * @return BaseResultVO
     */
    @RequestMapping(method = RequestMethod.POST, value = "/")
    @ResponseBody
    @OperateAction("O_PORTAL_USER_CREATE")
    public BaseResultVO allocateGroup(PortalUserAllocateGroupForm form) {
        return JsonResultUtil.getResult(portalUserOperateManager.allocateGroup(form));

    }

    /**
     * 查询用户组关联的用户信息
     *
     * @param form
     * @return BaseResultVO
     */
    @RequestMapping(method = RequestMethod.GET, value = "/page/group")
    @ResponseBody
    @OperateAction("O_PORTAL_USER_GET")
    public BaseResultVO queryUserPageByGroupIdAndCnd(PortalQueryByPageForm form) {
        return JsonResultUtil.getResult(portalUserQueryManager.queryUserPageByGroupIdAndCnd(form));

    }

    /**
     * 查询角色关联的用户信息
     *
     * @param form
     * @return BaseResultVO
     */
    @RequestMapping(method = RequestMethod.GET, value = "/page/role")
    @ResponseBody
    @OperateAction("O_PORTAL_USER_GET")
    public BaseResultVO queryUserPageByRoleIdAndCnd(PortalQueryByPageForm form) {
        return JsonResultUtil.getResult(portalUserQueryManager.queryUserPageByRoleIdAndCnd(form));

    }

    /**
     * 新增用户
     */
    @RequestMapping(method = RequestMethod.POST, value = "/add")
    @ResponseBody
    @OperateAction("ADD")
    @ApiOperation(value = "新增用户", notes = "新增用户。")
    public BaseResultVO add(@ModelAttribute PortalUserAddForm form) {
        return JsonResultUtil.getResult(portalUserOperateManager.add(form));
    }

    /**
     * 编辑用户
     */
    @RequestMapping(method = RequestMethod.PUT, value = "")
    @ResponseBody
    @OperateAction("UPDATE")
    @ApiOperation(value = "修改用户", notes = "修改用户信息。")
    public BaseResultVO update(@ModelAttribute PortalUserUpdateForm form) {
        return JsonResultUtil.getResult(portalUserOperateManager.update(form));
    }

    /**
     * 激活用户
     */
    @RequestMapping(method = RequestMethod.PUT, value = "/enabled/{id}")
    @ResponseBody
    @OperateAction("ENABLED")
    @ApiOperation(value = "激活用户", notes = "激活用户。")
    public BaseResultVO enabled(@PathVariable String id) {
        return JsonResultUtil.getResult(portalUserOperateManager.enabled(id));
    }

    /**
     * 禁用用户
     */
    @RequestMapping(method = RequestMethod.PUT, value = "/disabled/{id}")
    @ResponseBody
    @OperateAction("DISABLED")
    @ApiOperation(value = "禁用用户", notes = "禁用用户。")
    public BaseResultVO disabled(@PathVariable String id) {
        return JsonResultUtil.getResult(portalUserOperateManager.disabled(id));
    }

    /**
     * 删除用户
     */
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
    @ResponseBody
    @OperateAction("DELETE")
    @ApiOperation(value = "删除用户", notes = "删除用户。")
    public BaseResultVO delete(@PathVariable String id) {
        return JsonResultUtil.getResult(portalUserOperateManager.delete(id));
    }

    /**
     * 查询用户列表
     *
     * @param form
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/pageList")
    @ResponseBody
    @OperateAction("query")
    @ApiOperation(value = "分页查询用户列表", notes = "分页查询用户列表")
    public BaseResultVO queryPagelist(@ModelAttribute PortalUserQueryPageListForm form) {
        return JsonResultUtil.getResult(portalUserQueryManager.queryPageList(form));
    }

    /**
     * 重置用户密码
     *
     * @param userId
     * @return BaseResultVO
     */
    @RequestMapping(method = RequestMethod.POST, value = "/resetPW")
    @ResponseBody
    public BaseResultVO resetPassword(@RequestParam String userId) {
        return JsonResultUtil.getResult(portalUserOperateManager.resetPassword(userId));
    }

    /**
     * 修改用户密码
     *
     * @param form
     * @return BaseResultVO
     */
    @RequestMapping(method = RequestMethod.POST, value = "/modifyPW")
    @ResponseBody
    public BaseResultVO modifyPassword(@RequestBody ModifyPasswordForm form) {
        return JsonResultUtil.getResult(portalUserOperateManager.modifyPassword(form));
    }

    /**
     * 禁用token
     *
     * @param tokenId
     * @return BaseResultVO
     */
    @RequestMapping(method = RequestMethod.POST, value = "/forbiddenToken")
    @ResponseBody
    public BaseResultVO forbiddenToken(@RequestParam String tokenId) {
        return JsonResultUtil.getResult(portalUserOperateManager.forbiddenToken(tokenId));
    }

    /**
     * 查询未过期token
     *
     * @param tenantId,loginId
     * @return BaseResultVO
     */
    @RequestMapping(method = RequestMethod.POST, value = "/listToken")
    @ResponseBody
    public BaseResultVO listToken(@RequestParam String tenantId, @RequestParam String loginId) {
        return JsonResultUtil.getResult(portalUserOperateManager.listTokenByTenantIdAndLoginId(tenantId, loginId));
    }

    /**
     * 查询用户状态
     *
     * @return BaseResultVO
     */
    @RequestMapping(method = RequestMethod.GET, value = "/status")
    @ResponseBody
    public BaseResultVO getStatus() {
        return JsonResultUtil.getResult(portalUserQueryManager.getUserStatus());
    }

    /**
     * 上传用户数据的EXCEL，excel内容存入缓存
     *
     * @param request 请求
     * @return 返回请求处理结果。
     */
    @ApiOperation(value = "上传用户数据的EXCEL，存入缓存", notes = "上传用户列表EXCEL。")
    @RequestMapping(value = "/upload_users", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    @ResponseBody
    public BaseResultVO uploadUsers(HttpServletRequest request, @RequestParam String tenantId) {
        return JsonResultUtil.getResult(userExcelFileHandler.processUploadFile(request, tenantId));
    }

    /**
     * 根据excel名，返回上传预览
     *
     * @param fileName excel文件名
     * @return 返回请求处理结果。
     */
    @ApiOperation(value = "根据excel名，返回上传预览", notes = "根据excel名，返回上传预览。")
    @RequestMapping(value = "/importPriview/fileName", method = RequestMethod.GET, produces = "application/json; charset=UTF-8")
    @ResponseBody
    public BaseResultVO getUsersByExcelFileName(@RequestParam String fileName) {
        return JsonResultUtil.getResult(userExcelFileHandler.getUsersByExcelFileName(fileName));
    }

    /**
     * 导入预览中的合法数据，并删除用户列表缓存
     *
     * @param fileName excel文件名
     * @return 返回请求处理结果。
     */
    @ApiOperation(value = "导入预览中的合法数据，并删除用户列表缓存", notes = "导入预览中的合法数据，并删除用户列表缓存。")
    @RequestMapping(value = "/importPriview/fileName", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    @ResponseBody
    public BaseResultVO saveImportUsers(@RequestParam String fileName) {
        return JsonResultUtil.getResult(userExcelFileHandler.saveUsersByExcelFileName(fileName));
    }

    /**
     * 取消导入，直接删除用户列表缓存
     *
     * @param fileName excel文件名
     * @return 返回请求处理结果。
     */
    @ApiOperation(value = "取消导入，直接删除用户列表缓存", notes = "取消导入，直接删除用户列表缓存")
    @RequestMapping(value = "/importPriview/fileName", method = RequestMethod.DELETE, produces = "application/json; charset=UTF-8")
    @ResponseBody
    public BaseResultVO removeImportUsers(@RequestParam String fileName) {
        return JsonResultUtil.getResult(userExcelFileHandler.removeUsersByExcelFileName(fileName));
    }

    /**
     * 下载用户信息模板。
     *
     * @param request  请求
     * @param response 响应
     * @throws Exception
     * @throws IOException
     */
    @ApiOperation(value = "下载用户信息模板的接口", notes = "下载用户信息模板的EXCEL。")
    @RequestMapping(value = "/templete/download", method = RequestMethod.GET)
    public void downloadUserTemplet(HttpServletRequest request, HttpServletResponse response) throws Exception {
        userExcelTempleteHandler.downloadTemplete(request, response);
    }

    /**
     * 下载用户列表。
     *
     * @param request  请求
     * @param response 响应
     * @throws Exception
     * @throws IOException
     */
    @ApiOperation(value = "下载用户列表的接口", notes = "下载用户列表的EXCEL。")
    @RequestMapping(value = "/list/download", method = RequestMethod.GET)
    public void downloadUserList(HttpServletRequest request, HttpServletResponse response, @RequestParam String deptId,
                                 @RequestParam("tenantId") String tenantId) throws Exception {
        userExcelTempleteHandler.downloadData(response, deptId, tenantId);
    }

}
